<template>
    <div class='dialogSlider'>
        <publicDialog :props="dialogData"></publicDialog>
        <!-- Root element of PhotoSwipe. Must have class pswp. -->
        <div ref='dialogSlider' class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
            <!-- Background of PhotoSwipe. 
         It's a separate element, as animating opacity is faster than rgba(). -->
            <div class="pswp__bg"></div>
            <!-- Slides wrapper with overflow:hidden. -->
            <div class="pswp__scroll-wrap">
                <!-- Container that holds slides. PhotoSwipe keeps only 3 slides in DOM to save memory. -->
                <div class="pswp__container">
                    <!-- don't modify these 3 pswp__item elements, data is added later on -->
                    <div class="pswp__item"></div>
                    <div class="pswp__item"></div>
                    <div class="pswp__item"></div>
                </div>
                <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
                <div class="pswp__ui pswp__ui--hidden">
                    <div class="pswp__top-bar">
                        <!--  Controls are self-explanatory. Order can be changed. -->
                        <div class="pswp__counter"></div>
                        <button class="pswp__button pswp__button--flag" title="Flag" v-show="showFlag"></button>
                        <!-- <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                        <button class="pswp__button pswp__button--share" title="Share"></button>
                        <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                        <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button> -->
                        <!-- Preloader demo https://codepen.io/dimsemenov/pen/yyBWoR -->
                        <!-- element will get class pswp__preloader--active when preloader is running -->
                        <div class="pswp__preloader">
                            <div class="pswp__preloader__icn">
                                <div class="pswp__preloader__cut">
                                    <div class="pswp__preloader__donut"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--
                    <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                        <div class="pswp__share-tooltip"></div>
                    </div>
                    <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
                    </button>
                    <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
                    </button>
                    -->
                    <div class="pswp__caption">
                        <div class="pswp__caption__center"></div>
                    </div>
                    <div class="pswp__bottom-bar">
                        <div class="pswp__bottom--more">
                            <span><i></i><i></i><i></i></span>
                            <div class="pswp__bottom--moreList"></div>
                        </div>
                        <div class="pswp__button pswp__button--main" title="Main" v-show="showMain">{{language.dialogSlider_set_main}}</div>
                    </div>
                </div>
            </div>
        </div>
        <loading :props="{show:loading,type:'page'}"></loading>
    </div>
</template>
<script>
import PhotoSwipe from '../lib/photoSwipe/photoswipe'
import PhotoSwipeUI_Default from '../lib/photoSwipe/photoswipe-ui-default';
import * as API from '../api';
import publicDialog from './PublicDialog'
import loading from './Loading'

let pswp;
export default {

    name: 'dialogSlider',

    components: {
        publicDialog,
        loading
    },

    props: ['propsDialogSlider', 'propsList', 'propsIndex', 'propsDialogType'],

    data: function() {
        return {
            userInfo: this.$store.state.user.userInfo,
            showFlag: false,
            showMain: true,
            loading: false,
            dialogData: {},
        };
    },

    mounted: function() {
        //console.log('mounted', this.propsList);
        let tthis = this;
        pswp = new PhotoSwipe(this.$refs.dialogSlider, PhotoSwipeUI_Default, this.propsList, this.getOptions());
        pswp.listen('close', function() {
            setTimeout(function() {
                tthis.$emit('update:propsDialogSlider', false);
            }, 500);
        });
        pswp.listen('afterChange', function() {
            if (pswp.currItem.data && tthis.$store.state.user.userInfo.account.usr_id != pswp.currItem.data.prof_id) {
                tthis.showFlag = true;
            } else {
                tthis.showFlag = false;
            }
            tthis.showMain = parseInt(pswp.getCurrentIndex()) == 0 ? false : true;
            tthis.$emit('update:propsIndex', pswp.getCurrentIndex());
        });
        pswp.init();
    },

    methods: {

        getOptions() {
            let tthis = this,
                options = {
                    index: this.propsIndex,
                    getThumbBoundsFn: function(index) {
                        let thumbnail = tthis.$parent.$vnode.elm.querySelectorAll('dd')[index],
                            pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
                            rect = thumbnail.getBoundingClientRect();
                        return {
                            x: rect.left,
                            y: rect.top + pageYScroll,
                            w: rect.width
                        };
                    }
                };

            if (this.propsDialogType == 'edit-public') {
                options.moreEl = true;
                options.mainEl = true;
                options.mainPhoto = function() {
                    console.log('mainPhoto', pswp.getCurrentIndex());
                    tthis.setMainPhoto(pswp.getCurrentIndex());
                };
                options.optionPhoto = function() {
                    console.log('optionPhoto');
                    tthis.showBottomList();
                }
            } else if (this.propsDialogType == 'edit-private') {
                options.moreEl = true;
                options.optionPhoto = function() {
                    console.log('optionPhoto');
                    tthis.showBottomList();
                };
                options.movePhoto = function() {
                    // console.log('movePhoto', pswp.getCurrentIndex());
                    tthis.movePhoto(pswp.getCurrentIndex());
                };
            } else {
                options.getThumbBoundsFn = null;
                options.flagEl = true;
                options.flagShow = function() {
                    if (tthis.propsDialogType == 'message') {
                        tthis.dialogData = {
                            position: 'bottom',
                            content: {
                                btn: [{
                                    label: tthis.language.dialogs_porn,
                                    btnCallback: function() {
                                        tthis.reportMessagePhoto(tthis.language.dialogs_porn);
                                    }
                                }, {
                                    label: tthis.language.dialogs_fake,
                                    btnCallback: function() {
                                        tthis.reportMessagePhoto(tthis.language.dialogs_fake);
                                    }
                                }, {
                                    label: tthis.language.dialogs_other,
                                    btnCallback: function() {
                                        tthis.reportMessagePhoto(tthis.language.dialogs_other);
                                    }
                                }]
                            }
                        };
                    } else {
                        tthis.dialogData = {
                            position: 'bottom',
                            content: {
                                btn: [{
                                    label: tthis.language.dialogs_reportPhoto,
                                    btnCallback: function() {
                                        tthis.sureFlag();
                                    }
                                }]
                            }
                        };
                    }
                }
            }

            if (tthis.propsDialogType == 'letsMeet') {
                options.getThumbBoundsFn = null;
            }

            return options;
        },

        setMainPhoto: function(index) {
            let arr = this.propsList,
                tthis = this,
                obj = {};
            obj.from_id = arr[index].pid;
            obj.to_id = arr[0].pid;
            obj.index = index;
            obj.type = (this.propsDialogType == 'edit-public') ? 0 : 1;
            obj.callback = function() {
                pswp.items = tthis.exchangePhotoArr(pswp.items, index, 0);
                tthis.resetSlider();
                tthis.loading = false;
            };
            tthis.loading = true;
            if (obj.from_id !== obj.to_id) {
                this.$store.dispatch('user_setMainPhoto', obj);
            }
        },

        movePhoto: function(index) {
            let tthis = this,
                arr = this.propsList,
                obj = {},
                toAlbum;

            if (this.propsDialogType == 'edit-public') {
                toAlbum = 'to_extra';
                obj.type = 0;
            } else {
                toAlbum = 'to_pub'
                obj.type = 1;
            }

            obj.index = index;
            obj.data = {
                'action': toAlbum,
                'item_id': arr[index].pid
            }
            obj.callback = function() {
                if (pswp.items.length > 1) {
                    if (pswp.getCurrentIndex() == pswp.items.length - 1) {
                        pswp.goTo(pswp.getCurrentIndex() - 1);
                    } else {
                        pswp.goTo(pswp.getCurrentIndex());
                    }
                }
                pswp.items = tthis.removePhotoArr(pswp.items, index);
                tthis.resetSlider();
                tthis.loading = false;
            };
            this.loading = true;
            this.$store.dispatch('user_movePhoto', obj);
        },

        deletePhoto: function(index) {
            let arr = this.propsList,
                tthis = this,
                obj = {};
            obj.type = (this.propsDialogType == 'edit-public') ? 0 : 1;
            obj.item_id = arr[index].pid;
            obj.index = index;
            obj.callback = function() {
                // If you're going to remove current slide – call goTo method before. The official documentation
                if (pswp.items.length > 1) {
                    if (pswp.getCurrentIndex() == pswp.items.length - 1) {
                        pswp.goTo(pswp.getCurrentIndex() - 1);
                    } else {
                        pswp.goTo(pswp.getCurrentIndex());
                    }
                }
                pswp.items = tthis.removePhotoArr(pswp.items, index);
                tthis.resetSlider();
                tthis.loading = false;
            };
            tthis.loading = true;
            this.$store.dispatch('user_deletePhoto', obj);
        },

        resetSlider: function() {
            //console.log('resetSlider', pswp, pswp.items, pswp.getCurrentIndex());
            if (pswp.items.length < 1) {
                pswp.close();
            }
            pswp.invalidateCurrItems();
            pswp.updateSize(true);
            pswp.ui.update();
            this.$parent.$forceUpdate();
        },

        exchangePhotoArr: function(arr, fromNum, toNum) {
            var temp = arr[toNum];
            arr[toNum] = arr[fromNum];
            arr[fromNum] = temp;
            return arr;
        },

        removePhotoArr: function(arr, num) {
            arr.splice(num, 1);
            return arr;
        },

        sureFlag() {
            let tthis = this,
                data = pswp.currItem.data;
            data.type = 10;
            data.text = 'From MOBILE: Bad photo';
            API.report({
                data: data,
                success: function(result) {
                    if (result.results == 1) {
                        tthis.dialogData = {
                            position: 'top',
                            type: 'successful',
                            content: {
                                info: tthis.language.unify_reportedSuccess
                            }
                        }
                    } else if (result.errcode) {
                        tthis.dialogData = {
                            position: 'top',
                            type: 'error',
                            content: {
                                info: result.errmsg
                            }
                        }
                    }
                }
            });
        },

        reportMessagePhoto(reason) {
            let tthis = this,
                data = pswp.currItem.data;
            data.photo_url = pswp.currItem.src;
            data.reason = reason;
            API.report_message_photo({
                data: data,
                success: function(result) {
                    if (result.results == 1) {
                        tthis.dialogData = {
                            position: 'top',
                            type: 'successful',
                            content: {
                                info: tthis.language.unify_reportedSuccess
                            }
                        }
                    } else if (result.errcode) {
                        tthis.dialogData = {
                            position: 'top',
                            type: 'error',
                            content: {
                                info: result.errmsg
                            }
                        }
                    }
                }
            });
        },

        showBottomList() {
            var tthis = this;
            this.dialogData = {
                position: 'bottom',
                content: {
                    btn: [{
                        label: this.propsDialogType == 'edit-public' ? this.language.dialogSlider_make_private : this.language.dialogSlider_make_public,
                        btnCallback: function() {
                            tthis.movePhoto(pswp.getCurrentIndex());
                        }
                    }, {
                        label: this.language.dialogSlider_delete,
                        btnCallback: function() {
                            tthis.deletePhoto(pswp.getCurrentIndex());
                        }
                    }]
                }
            };
        },
    }

}

</script>
<style scoped>
@import '../lib/photoSwipe/photoswipe.css';
@import '../lib/photoSwipe/default-skin/default-skin.css';

</style>
